﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using morningstone;

namespace TapCSharep
{
    class Trader : ITapTradeAPINotify
    {   
        //交易接口
        private  static ITapTradeAPI m_pAPI;


        public int TAPIERROR_SUCCEED { get; private set; }


        public Trader()
        {
            
        }

        ~Trader()
        {
            Dispose();
        }

        public void CreateMDApi()
        {
            if (m_pAPI == null)
            {
                TapAPIApplicationInfo stAppInfo = new TapAPIApplicationInfo();
                stAppInfo.AuthCode = "67EA896065459BECDFDB924B29CB7DF1946CED32E26C1EAC946CED32E26C1EAC946CED32E26C1EAC946CED32E26C1EAC5211AF9FEE541DDE41BCBAB68D525B0D111A0884D847D57163FF7F329FA574E7946CED32E26C1EAC946CED32E26C1EAC733827B0CE853869ABD9B8F170E14F8847D3EA0BF4E191F5D97B3DFE4CCB1F01842DD2B3EA2F4B20CAD19B8347719B7E20EA1FA7A3D1BFEFF22290F4B5C43E6C520ED5A40EC1D50ACDF342F46A92CCF87AEE6D73542C42EC17818349C7DEDAB0E4DB16977714F873D505029E27B3D57EB92D5BEDA0A710197EB67F94BB1892B30F58A3F211D9C3B3839BE2D73FD08DD776B9188654853DDA57675EBB7D6FBBFC";

                stAppInfo.KeyOperationLogPath = "\\log";

                IntPtr cPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(sizeof(int));
                System.Runtime.InteropServices.Marshal.WriteInt32(cPtr, 1);
                SWIGTYPE_p_int s = new SWIGTYPE_p_int(cPtr, false);
                m_pAPI = iTapTradeAPI_CPP.CreateITapTradeAPI(stAppInfo, s);
                if (m_pAPI == null)
                {
                    Console.WriteLine("api创建失败:");
                }
                else
                {
                    Console.WriteLine("创建成功");
                }

                System.Runtime.InteropServices.Marshal.FreeHGlobal(cPtr);
                SetAPINotify();
                connect();
            }
        }

        public void SetAPINotify()
        {
            m_pAPI.SetAPINotify(this);
        }

        public void connect()
        {
             int  iErr =m_pAPI.SetHostAddress("61.163.243.173", 8383);
            if (TAPIERROR_SUCCEED != iErr)
            {
                Console.WriteLine("SetHostAddress Error:");
                return;
            }
            else
            {
                Console.WriteLine("连接成功:");
            }
            TapAPITradeLoginAuth stLoginAuth=new TapAPITradeLoginAuth() ;
            stLoginAuth.UserNo = "Q576288330";
            stLoginAuth.Password = "123456";
            stLoginAuth.ISModifyPassword = 'N';
            iErr = m_pAPI.Login(stLoginAuth);
            if (TAPIERROR_SUCCEED != iErr)
            {
                Console.WriteLine("Login Error:");
                return;
            }

            Console.WriteLine("Login 成功:");

        }
        public override void OnConnect()
        {
            Console.WriteLine("建立连接成功");
        }

        public override void OnRspLogin(int errorCode, TapAPITradeLoginRspInfo loginRspInfo)
        {
            Console.WriteLine("登录成功");
        }

        public override void OnDisconnect(int reasonCode)
        {
            Console.WriteLine("连接建立失败");
        }

        public override void OnAPIReady(int errorCode)
        {
            Console.WriteLine("交易接口准备就绪");
            insertOrder();
        }


        public override void OnRspOrderAction(uint sessionID, int errorCode, TapAPIOrderActionRsp info)
        {
            Console.WriteLine("OnRspOrderAction");
        }
        public override void OnRtnPositionProfit( TapAPIPositionProfitNotice info)
        {
           
        }

        public override void OnRtnOrder(TapAPIOrderInfoNotice info)
        {
            if (info.OrderInfo != null)
            {
                Console.WriteLine("委托回报：" + info.OrderInfo.OrderNo + "  " + info.OrderInfo.OrderType + " " + info.OrderInfo.StopPrice);
            }
            else
            {
                Console.WriteLine("委托回报为空：");
            }
            
        }

        public override void OnRtnFill(TapAPIFillInfo info)
        {
            Console.WriteLine("成交回报："+ info.OrderNo + "  "+ info.AccountNo+" "+ info.ContractNo+" "+ info.ExchangeNo+" "+ info.FeeValue);
        }


        public override void OnRtnPositionSummary(SWIGTYPE_p_TapAPIPositionSummary info)
        {
            Console.WriteLine("OnRtnPositionSummary");
        }

        public override void OnRtnPosition(TapAPIPositionInfo info)
        {
            Console.WriteLine("TapAPIPositionInfo");
        }
        public static void insertOrder()
        {
         
            //下单
            TapAPINewOrder stNewOrder = new TapAPINewOrder();
            stNewOrder.AccountNo = "Q576288330";
            stNewOrder.ExchangeNo = "NYMEX";
            stNewOrder.CommodityType = 'F';
            stNewOrder.CommodityNo = "CL";
            stNewOrder.ContractNo = "2401";
            stNewOrder.OrderType = '1';
            stNewOrder.OrderSource = '6';
            stNewOrder.TimeInForce = '0';
            stNewOrder.OrderSide = 'B';
            stNewOrder.OrderPrice = 0;
            stNewOrder.OrderQty = 1;


            IntPtr cPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(sizeof(int));
            System.Runtime.InteropServices.Marshal.WriteInt32(cPtr, 0);

            SWIGTYPE_p_unsigned_int s = new SWIGTYPE_p_unsigned_int(cPtr, false);
            SWIGTYPE_p_a_51__char pp = new SWIGTYPE_p_a_51__char(cPtr, false);
            int iErr = m_pAPI.InsertOrder(s, pp, stNewOrder);
            //释放内存
            System.Runtime.InteropServices.Marshal.FreeHGlobal(cPtr);
            if (0 != iErr)
            {
                return;
            }
            else
            {
                Console.WriteLine("委托成功@@@@@@@@@@@@@@@@@@@");
            }
        }


    }
}
